<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>角色管理</title>
<script src="/res/js/common/jquery.1.9.1.min.js" type="text/javascript"></script>
<script src="/res/js/structure/struture.js" type="text/javascript"></script> 
<script src="/res/js/common/common.js" type="text/javascript"></script>
<script src="/res/third/layer/layer.js" type="text/javascript"></script>
<link href="/res/css/respective/role/role.css" rel="stylesheet">
</head>
[#assign menucode="roleManage"/]
<body>
	<div class="warp">
		[#include "/front/common/front_headAzt.html"]
		<div class="backcontent">
			[#include "/front/common/front_leftAzt.html"]
			<div class="back-main">
				<div class="mainContent">
		<div class="mainContentWhite">
			<h1>角色管理</h1>
			<hr>
		<form id="roleForm" onsubmit="return false;">
			<div class="branchInfo">
			    <input id="roleId" type="hidden" value="[#if role??]${role.id!}[/#if]"  /> 
			    <input id="isdefault" type="hidden" value="[#if isdefault??]${isdefault!}[#else]2[/#if]"  /> 
				<div>
					<label style="padding-left: 15px;text-align: left;width: auto;"><span>*</span>角色名称：</label> <input id="roleName" name="roleName" maxlength="8" type="text" value="[#if role??]${role.name!}[/#if]" placeholder="请填写角色名称"/>
				</div>
				<div class="addPurRole">
					<label style="padding-left: 15px;text-align: left;">您可能需要添加的角色：</label>
					[#if defaultRole?? && defaultRole?size>0]
					[#list defaultRole as defaultRole]
					[#if defaultRole.ifAdmin==0]
					<label class="defaultRole" roleId="${defaultRole.id!}" [#if  role?? && defaultRole?? && role.id==defaultRole.id ]style="color:#f9a32a;"[/#if]>${defaultRole.name!}</label>
					[/#if]
					[/#list]
					[/#if]
				</div> 
			</div>
			<h2>
				<span>*</span>角色菜单权限
			</h2>
			                 [#if loadMenuTree??]
			                 [#list loadMenuTree as par]
							<div class="itermBorder">
							<!-- 角色选择时不显示-->
							   <b [#if role??][#if par.mark==1 ] style="display: none;"[/#if][#else]style="display: none;"[/#if]> <i class="noChoose"></i> <bdo
										data-type="fun_${par.code!}">
										<strong>${par.name!}</strong>
										<p>${par.descr!}</p>
									</bdo>
								</b>
								
								<div class="verAlignBdoDiv" [#if role??][#if par.mark!=1 ] style="display: none;"[/#if][/#if] >
									<div class="verAlignBdo">
										<b class="tbPad"> <i class="choose" parentId="${par.id!}"></i> <bdo
												data-type="fun_${par.code!}">
												<strong>${par.name!}</strong> <small>${par.descr!}</small>
											</bdo>
										</b>
									</div>
									<div class="power">
									[#list par.children as child]
										<i data-type="fun_${child.code!}" childid="${child.id!}">  <img class="visimg" [#if child.mark!=0 ]style="visibility: visible;"[#else]style="visibility: hidden;"[/#if] src="/res/images/role/b_03.png" />
											<p>${child.name!}</p>
										</i>
									[/#list]
									</div>
								</div>
							</div>
							[/#list]
						  [/#if]
			   <button class="blueButt" onclick="saverole(1);">保存</button>
			   <button  onclick="saverole(2)" class="addJixu organgeButt" style="margin-left: 20px;">保存并继续添加</button>
			   <button type="button" onclick="cancle();" class="cancle">返回</button>
		</form>
	</div>
</div>
			</div>
			<!-- 底部菜单-->
			[#include "/front/common/front_footerAzt.html"]
		</div>
	</div>
</body>
<script src="/res/third/nice-validator/jquery.validator.js?local=zh-CN" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
	$("#roleForm").validator({
	    theme: 'newsimple_right',
	    timely: 1,
	    stopOnError: true,
	    rules:{
	    	uniqueName:function(){
	    		return $.ajax({
					type : "post",
					dataType : "json",
					url : "/orgstruct/checkrolename",
					data :{'rolename':$.trim($("#roleName").val()),'roleId':$("#roleId").val()},
					success : function(data) {//成功的时候调用
					}
				});
	    	}
	    },
	    fields: {
	    	'roleName':"required;uniqueName",
	    },
        messages: {
        required: "必填",
        }
    });
	
	$(".defaultRole").click(function(){
		var defaultRoleId=$(this).attr("roleId");
		location.href="/orgstruct/addrole?roleId="+defaultRoleId+"&isdefault=1";
	});
});

function saverole(urlobj){
	var data={};
	var information=[];
	var parent={};
	$(".itermBorder").each(function(){
		var child=[];
		var verAlignBdoDiv=$(this).find(".verAlignBdoDiv :visible");
		if(verAlignBdoDiv.length>0){
			var childcount=0;
			var parentId=verAlignBdoDiv.find(".choose").attr("parentId");
			$(this).find(".power i").each(function(){
				//判断子存在
				if($(this).children("img").css("visibility")!="hidden"){
					child.push($(this).attr("childid"));
					childcount++;
				}
			});
			if(childcount>0){
				parent[parentId]=child;
			}
		}
	});
	
	data.parent=parent;
	data.roleName=$.trim($("#roleName").val());
	data.roleId=$("#roleId").val();
	data.isdefault=$("#isdefault").val();
	
	var parentlength=false;
    for (var key in parent) { 
    	parentlength=true;
    	continue;
    } 
	
	$("#roleForm").validator({
		    valid: function(form){
		    	if(!parentlength){
		    		if(urlobj!=''){
		    		layer.msg("请选择角色权限菜单" , {time:2000} );
		    		}
		    		return;
		    	}
		    	 var me = this;
			     me.holdSubmit();
		    	$.ajax({
					type : "post",
					dataType : "json",
					url : "/orgstruct/saverole",
					data :{'data' :JSON.stringify(data)},
					success : function(data) {//成功的时候调用
						if(data.success){
							 me.holdSubmit(true);
							 layer.msg("保存成功" , {time:3000} );
									 if(urlobj==1){
										 setTimeout(function(){
									     location.href="/orgstruct/roleManage"} , 3000 );
										 }else{
										 setTimeout(function(){
										 location.href="/orgstruct/addrole"}, 3000 );
										 }
							
						}else{
							 me.holdSubmit(false);
							 layer.msg(data.msg , {time:2000} );
						}
					 }
				});
		    }
	}); 
}

function cancle(){
	location.href="/orgstruct/roleManage";
}
</script>
</html>
